Recreate named boundaries on change in alignment
The below code snippet shows recreating the named boundaries because of change in alignment. This code deletes all the existing named boundaries from the DGN and creates the named boundaries using the alignment. The alignment assumed as updated and hard coded values are used here in the code for alignment selection and for number of named boundaries.
//Required References
using System;
using System.Diagnostics;
using Bentley.GeometryNET;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using System.Collections.Generic;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.LinearGeometry;
public void RecreateNBOnChangeInAlignment()
{
try
{
//Get active DGN
DgnModelRef dgnModelRef = Session.Instance.GetActiveDgnModelRef();
DgnFile dgnFile = dgnModelRef.GetDgnFile();
//Delete all the previous named boundaries
ModelIndexCollection modelIndexCollection = dgnFile.GetModelIndexCollection();
foreach (ModelIndex modelIndex in modelIndexCollection)
{
DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt status, modelIndex.Id);
if (dgnModel.ModelType == DgnModelType.Normal)
{
//Get Named Boundary Collection using DgnModel
NamedBoundaryCollection namedBoundaries = new NamedBoundaryCollection(dgnModel);
IEnumerator<NamedBoundary> namedBoundaryIterator = namedBoundaries.GetEnumerator();
//Delete all Named boundaries
while (namedBoundaryIterator.MoveNext())
{
NamedBoundary namedBoundaryToDelete = namedBoundaryIterator.Current;
namedBoundaryToDelete.Delete();
namedBoundaryIterator.Reset();
}
}
}
//Recreate the named boundaries on alignment
//Get active DGN model and Geometric model to get the alignment
DgnModel model = Session.Instance.GetActiveDgnModel();
Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
GeometricModel geomModel = con.GetActiveGeometricModel();
if (geomModel == null) return;
//Get the alignment with name specified here in sampleAlignmentName variable
//It is assumed that the sampleAlignmentName alignment has changes and user needs to recreate the named boundaries on it
foreach (Alignment alignment in geomModel.Alignments)
{
string sampleAlignmentName = "SampleAlignment";
if (alignment.Name == sampleAlignmentName)
{
List<DPoint3d> dPoint3Ds = new List<DPoint3d>();
LinearPoint startPoint = alignment.LinearGeometry.StartPoint;
LinearPoint endPoint = alignment.LinearGeometry.EndPoint;
double length = alignment.LinearGeometry.Length;
double distance = length / 10;
int count = 1;
double startDistance = startPoint.DistanceAlong;
double endDistance = startPoint.DistanceAlong + distance;
while (startDistance < endPoint.DistanceAlong)
{
//Get points at distance offset for named boundary creation based on start distance and end distance
LinearPoint point1 = alignment.LinearGeometry.GetPointAtDistanceOffset(startDistance, 50);
LinearPoint point2 = alignment.LinearGeometry.GetPointAtDistanceOffset(startDistance, -50);
LinearPoint point3 = alignment.LinearGeometry.GetPointAtDistanceOffset(endDistance, -50);
LinearPoint point4 = alignment.LinearGeometry.GetPointAtDistanceOffset(endDistance, 50);
LinearPoint point5 = point1;
//User might need unit conversion here on points based on Unit settings in DGN file
DPoint3d[] nbPoints = new DPoint3d[5];
nbPoints[0] = point1.Coordinates;
nbPoints[1] = point2.Coordinates;
nbPoints[2] = point3.Coordinates;
nbPoints[3] = point4.Coordinates;
nbPoints[4] = point5.Coordinates;
//Create new Shape Element from points
Bentley.DgnPlatformNET.Elements.ShapeElement nbShape = new Bentley.DgnPlatformNET.Elements.ShapeElement(model, null, nbPoints);
//Add newly created shape to the Active Dgn Model
nbShape.AddToModel();
//Create new instance of Named Boundary object and add shape as graphical element to NB
NamedBoundary namedBoundary = new NamedBoundary();
namedBoundary.Name = "NamedBoundary : " + count;
namedBoundary.ModelRef = model;
namedBoundary.GraphicalElement = nbShape;
//Save Named Boundary
if (StatusInt.Success != namedBoundary.Save())
return;
//Create new Named Boundary Group
NamedBoundaryGroup nbGroup = Bentley.DgnPlatformNET.NamedBoundaryGroup.CreateNamedBoundaryGroup(model, "NbGroup", "NbGroup");
//Insert Named Boundary in Named Boundary Group
nbGroup.InsertBoundary(namedBoundary);
//Write Named Boundary Group to file
if (StatusInt.Success != nbGroup.WriteToFile())
return;
//Update the start and end distance for next named boundary creation
startDistance = startDistance + distance;
endDistance = endDistance + distance;
}
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}